﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace HuseData
{
    public class GetHouseData
    {
        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="datatype">数据类型 5:住宅均价 2:已售套数 3:已售面积</param>
        /// <returns></returns>
        public List<HouseDataEntity> GetDataList(int datatype)
        {

            string rTxt = Common.GetPage_Html(string.Format(DataSoureUrl, datatype));


            string[] DataDayArray = getbetweenstring(rTxt, "arr=[,", "];").Replace("'", "").Split(',');

            string[] DataNumArray = getbetweenstring(rTxt, "];", "arr.shift();").Trim().Replace("\r\n","").Replace(";",",;").Split(';');

            int starti = 0; //开始日期位置
            if (DataNumArray.Length > 0)
            {
                starti = Convert.ToInt32(getbetweenstring(DataNumArray[0], "[", "]").Replace("'", ""));
            }


            List<HouseDataEntity> lst = new List<HouseDataEntity>();
            int tempj = 0;
            for (int i = starti; i < DataDayArray.Length; i++)
            {
                HouseDataEntity hde0 = new HouseDataEntity();                
                hde0.Area = 0;
                hde0.DataType = datatype;
                hde0.Day = Convert.ToDateTime(string.Format("{0}/{1}", GetYear(DataDayArray[i]), DataDayArray[i]));
                hde0.Num = Convert.ToDouble(getbetweenstring(DataNumArray[tempj],"=",","));
                lst.Add(hde0);
                tempj++;

                HouseDataEntity hde1 = new HouseDataEntity();
                hde1.Area = 1;
                hde1.DataType = datatype;
                hde1.Day = Convert.ToDateTime(string.Format("{0}/{1}", GetYear(DataDayArray[i]), DataDayArray[i]));
                hde1.Num = Convert.ToDouble(getbetweenstring(DataNumArray[tempj], "=", ","));
                lst.Add(hde1);
                tempj++;

                HouseDataEntity hde2 = new HouseDataEntity();
                hde2.Area = 2;
                hde2.DataType = datatype;
                hde2.Day = Convert.ToDateTime(string.Format("{0}/{1}", GetYear(DataDayArray[i]), DataDayArray[i]));
                hde2.Num = Convert.ToDouble(getbetweenstring(DataNumArray[tempj], "=", ","));
                lst.Add(hde2);
                tempj++;
                
            }

            return lst;
        }

        public string getbetweenstring(string txt, string start, string end)
        {
            if (!string.IsNullOrEmpty(txt))
            {
                int tempi = txt.IndexOf(start);
                txt = txt.Remove(0, tempi + start.Length);
                tempi = txt.IndexOf(end);
                txt = txt.Substring(0, tempi);
            }
            return txt;
        }


        public int GetYear(string month)
        {


            if (Convert.ToInt32(month.Split('/')[0]) == 12 && DateTime.Now.Month == 1)
            {
                return DateTime.Now.AddYears(-1).Year;
            }
            else
            {
                return DateTime.Now.Year;
            }
        }

        /// <summary>
        /// 数据来源网址
        /// </summary>
        static string DataSoureUrl = "http://www.fangdi.com.cn/circleVml.asp?qryType={0}";

    }
}
